﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

using StudentEFConsoleApp.EntityFramework.ExtensionMethods;
using SchoolSTEDAL.STEEntities;
using StudentEFConsoleApp.SchoolServiceReference;

namespace StudentEFConsoleApp
{
    public class STEInDisconnectedScenario
    {
       
        public static void AddDemo()
        {
            Student student = new Student();
            student.StudentName = "STEInDisconnectedScenario Student 1";

            Standard std = new Standard();
            std.StandardName = "STEInDisconnectedScenario standard 1";
            std.Description = "Added from wcf service";

            student.Standard = std;

            StudentAddress sAddress = new StudentAddress();
            sAddress.Address1 = "STEInDisconnectedScenario Address1";
            sAddress.Address2 = "STEInDisconnectedScenario Address2";
            sAddress.City = "STEInDisconnectedScenario city";
            sAddress.State = "STEInDisconnectedScenario state";

            student.StudentAddress = sAddress;

            Teacher tchr = new Teacher();
            tchr.TeacherName = "STEInDisconnectedScenario teacher1";
            tchr.Standard = std;

            Course cs1 = new Course();
            cs1.CourseName = "STEInDisconnectedScenario Course 1";
            cs1.Location = "City test";
            cs1.Teacher = tchr;

            Course cs2 = new Course();
            cs2.CourseName = "STEInDisconnectedScenario Course 2";
            cs2.Location = "City test";
            cs2.Teacher = tchr;


            student.Courses.Add(cs1);
            student.Courses.Add(cs2);


            using (SchoolServiceClient cl = new SchoolServiceClient())
            {
                cl.UpdateStudent(student);
            }

            Console.WriteLine("STEInDisconnectedScenario AddDemo Successfull");

        }

        public static void UpdateDemo()
        {
            using (SchoolServiceClient cl = new SchoolServiceClient())
            {

                Standard std = cl.GetStandard("STEInDisconnectedScenario standard 1");

                foreach (Teacher tchr in std.Teachers)
                    tchr.TeacherName = "Updated teacher name";

                std.StandardName = "Updated STEInDisconnectedScenario standard 1";

                cl.UpdateStandard(std);

            }
            Console.WriteLine("STEInDisconnectedScenario UpdateDemo Successfull");
        }

        public static void DeleteDemo()
        {
            using (SchoolServiceClient cl = new SchoolServiceClient())
            {
                
                Student student = cl.GetStudent("STEInDisconnectedScenario Student 1");
                
                student.StudentName = "Updated STEInDisconnectedScenario Student 1";

                if (student.Courses.Count > 1)
                    student.Courses.RemoveAt(0); // removing first course in Many-toMany relation

                if (student.StudentAddress != null)
                    student.StudentAddress.MarkAsDeleted<StudentAddress>(); //Deleting student address in One-to-One relation

                cl.UpdateStudent(student);
                
            }
            Console.WriteLine("STEInDisconnectedScenario DeleteDemo Successfull");

        }

    }
}
